import {createPinia} from "pinia/dist/pinia";

// const pinia = createPinia()
// app.use(pinia.use(piniaPluginPersist))
// import {useBdStore} from '@/stores/bdStore.js';
// import piniaPluginPersist from "pinia-plugin-persist";
//
// const useStore = useBdStore()

function formatDate(dateStr) {
  // 使用Date.parse将字符串转换为Date对象，并纠正时区
  const date = new Date(Date.parse(dateStr) + (Date.parse("Jan 1 1970 00:00:00 GMT+0800 (中国标准时间)") - Date.parse("Jan 1 1970 00:00:00 GMT+0000")));
  // 获取年、月、日
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从0开始的
  const day = date.getDate().toString().padStart(2, '0');
  // 返回年-月-日格式
  return `${year}-${month}-${day}`;
}

const tools = {
// 1 检查key
    async verfiyAppCdKey() {
        return new Promise((resolve, reject) => {
            window.pywebview.api.pymaterial_getTbVariables("appcdkey").then(async (r) => {
                if (r['code'] == -1000) {       //未输入key
                    resolve(-1000)
                }
                if (r.code == 0) {
                    //验证
                    if (await this.__appcdkeyHandle(r.requestdata[3]) == false) {
                        resolve(-10010)   //key过期
                    } else {
                        resolve(200)      //key正常
                    }
                }
            })
        })
    },
    
    async __appcdkeyHandle(appcdkeyStr)  {
        return new Promise((resolve, reject) => {
            if (appcdkeyStr == '') {
                useStore.app_usage_time_status = 2      //需要注册
                useStore.setAppUsageTimeTxt("无效的卡密", 2)
                resolve(false)
            }
            window.pywebview.api.py_unAppcodekey(appcdkeyStr).then((r) => {
                if (r.status == true) {
                    useStore.app_usage_time_status = 1
                    useStore.setAppUsageTimeTxt('有效时间:' + r.msg, 1)
                    console.log('有效时间:' + r.msg)    //有效时间
                    resolve(true)
                } else {
                    window.pywebview.api.pymaterial_updateTbVariables({
                        'key': 'appcdkey',
                        'value': '',
                        'category': 'app'
                    });
                    useStore.app_usage_time_status = 2      //需要注册
                    useStore.setAppUsageTimeTxt(r.msg, 2)
                    resolve(false)
                }
            })
        })
    },


// 2 是否同意免责
    async disclaimerFunc() {
        return new Promise((resolve, reject) => {
            window.pywebview.api.pymaterial_getTbVariables('disclaimer_agree').then(async (r) => {
                if (r.code == -1000) {
                    useStore.disclaimer_agree = false
                    resolve(false)    //未同意免责
                } else {
                    resolve(true)    //同意免责
                }
            })
        })
    },


//3 试用期
    async getpyFirstTime(day) {

        let first_start = {}
        return new Promise((resolve, reject) => {
            window.pywebview.api.py_getFirstStart().then((r) => {
                first_start['status'] = false
                first_start['msg'] = ""

                if (r['code'] == 0) {
                    let dateString = r['reqdata']
                    let inputDate = new Date(dateString.replace(/-/g, '/'));
                    let date2 = new Date(inputDate.setDate(inputDate.getDate() + day));

                    if (date2 > new Date()) {
                        first_start['status'] = true
                        first_start['msg'] = "到期时间：" + formatDate(date2)
                    } else {
                        first_start['status'] = false
                        first_start['msg'] = "已经过期：" + formatDate(date2)
                    }
                }
                console.log("first_start==", first_start)
                resolve(first_start)
            })
        })
    }

}

export default tools